package com.chexiehui.jc.jpush;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.AppApplication;
import com.chexiehui.jc.event.ReceiveRegistrationIdEvent;
import com.chexiehui.jc.ui.activity.ForumDynamicDetailsActivity;
import com.chexiehui.jc.ui.activity.MessageListActivity;
import com.chexiehui.jc.ui.activity.NewCarListActivity;
import com.chexiehui.jc.ui.activity.SpecialCarDetailsActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.SerializableSpTools;
import com.chexiehui.jc.utils.ThreadUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class PushMessageReceiver extends JPushMessageReceiver {
    private static final String TAG = "PushMessageReceiver";

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG, "[onMessage] " + customMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageOpened] " + message);
        try {
            JpushMessage jpushMessage = getProcessMessage(message);
            if(jpushMessage.getModelname() != null){
                if(jpushMessage.getModelname().equals("2")){
                    //打开自定义的Activity
                    Intent i = new Intent(context, SpecialCarDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("id", jpushMessage.getModelid());
                    i.putExtras(bundle);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }else if(jpushMessage.getModelname().equals("3")){
                    //打开自定义的Activity
                    Intent i = new Intent(context, ForumDynamicDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("postId", jpushMessage.getModelid());
                    i.putExtras(bundle);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }
            }
        } catch (Throwable throwable) {

        }
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        if (nActionExtra == null) {
            Log.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
            return;
        }
        if (nActionExtra.equals("my_extra1")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
        } else if (nActionExtra.equals("my_extra2")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
        } else if (nActionExtra.equals("my_extra3")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
        } else {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageArrived] " + message);
        processCustomMessage(message);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageDismiss] " + message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG, "[onRegister] " + registrationId);
        ThreadUtil.runInUiThread(() -> EventBus.getDefault().post(new ReceiveRegistrationIdEvent(registrationId)));
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG, "[onConnected] " + isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG, "[onCommandResult] " + cmdMessage);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    private void processCustomMessage(NotificationMessage message) {
        JpushMessage jpushMessage = new JpushMessage();
        jpushMessage.setMessageId(message.msgId);
        jpushMessage.setContent(message.notificationContent);
        jpushMessage.setTitle(message.notificationTitle);
        jpushMessage.setNoread(true);
        jpushMessage.setUserId(SerializableSpTools.getUserInfo().getId());
        if (!StringUtils.isEmpty(message.notificationExtras)) {
            try {
                JSONObject extraJson = new JSONObject(message.notificationExtras);
                if (extraJson.length() > 0) {
                    jpushMessage.setModelid(extraJson.getString("modelid"));
                    jpushMessage.setModelname(extraJson.getString("modelname"));
                }
                AppApplication.getInstances()
                        .getJpushMessageDao().insertOrReplace(jpushMessage);
                ThreadUtil.runInThread(() -> {
                    EventBus.getDefault().post(jpushMessage);
                });
            } catch (JSONException e) {
            }
        }
    }

    //send msg to MainActivity
    private JpushMessage getProcessMessage(NotificationMessage message) {
        JpushMessage jpushMessage = new JpushMessage();
        jpushMessage.setMessageId(message.msgId);
        jpushMessage.setContent(message.notificationContent);
        jpushMessage.setTitle(message.notificationTitle);
        jpushMessage.setNoread(true);
        jpushMessage.setUserId(SerializableSpTools.getUserInfo().getId());
        if (!StringUtils.isEmpty(message.notificationExtras)) {
            try {
                JSONObject extraJson = new JSONObject(message.notificationExtras);
                if (extraJson.length() > 0) {
                    jpushMessage.setModelid(extraJson.getString("modelid"));
                    jpushMessage.setModelname(extraJson.getString("modelname"));
                }
            } catch (JSONException e) {
            }
        }
        return jpushMessage;
    }

    @Override
    public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
        super.onNotificationSettingsCheck(context, isOn, source);
        Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
    }

}
